/*
 * Shape.cpp
 *
 *  Created on: Jan 5, 2014
 *      Author: nick
 */

//Header files
#include "Shape.h"

/*************************************************************************//**
 * @author Nick Neisen
 *
 * @par Description:
 * Constructor for a blank shape.
 *
 ****************************************************************************/
Shape::Shape() {
	//Initialize the offsets
	box.x = 0;
	box.y = 0;
	box.w = DOT_WIDTH;
	box.h = DOT_HEIGHT;

	//Initialize the velocity
	yVel = 0.0;
	xVel = 0.0;

	//Initialize the jump
	jump = 0.0;

	type = 0;
}

/*************************************************************************//**
 * @author Nick Neisen
 *
 * @par Description:
 * Destructor
 *
 ****************************************************************************/
Shape::~Shape() {
}

/*************************************************************************//**
 * @author Nick Neisen
 *
 * @par Description:
 * Outputs the two (x,y) coordinates stored within the class.  Made virtual
 * so children may print their extra class variables.
 *
 ****************************************************************************/
void Shape::Print() {
	//Output both (x,y) coordinates
	cout << endl;
}

void Shape::handle_input() {

	//If a key was pressed
	if (event.type == SDL_KEYDOWN) {
		//Adjust the velocity
		switch (event.key.keysym.sym) {
		case SDLK_SPACE:
		case SDLK_UP:
		case SDLK_DOWN:
		case SDLK_LEFT:
		case SDLK_RIGHT:
		default:
			break;
		}
	}
	//If a key was released
	else if (event.type == SDL_KEYUP) {
		//Adjust the velocity
		switch (event.key.keysym.sym) {
		case SDLK_SPACE:
		case SDLK_UP:
		case SDLK_DOWN:
		case SDLK_LEFT:
		case SDLK_RIGHT:
		default:
			break;
		}
	}
}

void Shape::move(Tile *tiles[]) {
	//Move the dot left or right
	box.x += xVel;

	//If the dot went too far to the left or right or touched a wall
	if ((box.x < 0.0) || (box.x + DOT_WIDTH > LEVEL_WIDTH)
			|| touches_wall(box, tiles)) {
		//move back
		box.x -= xVel;
	}

	//Move the dot up or down
	box.y += yVel;

	//If the dot went too far up or down or touched a wall
	if ((box.y < 0) || (box.y + DOT_HEIGHT > LEVEL_HEIGHT)
			|| touches_wall(box, tiles)) {
		//move back
		box.y -= yVel;
	}
}

void Shape::gravity(Tile *tiles[], int t) {
	//Y = Yi + Viyt - (1/2)gt2

	//Gravity
	yVel = yVel - (yVel * t) + (.5 * 9.8 * t * t);

	//If the dot went too far down or touched a wall
	if ((box.y < 0) || (box.y + DOT_HEIGHT > LEVEL_HEIGHT)
			|| touches_wall(box, tiles)) {
		//move back
		yVel = yVel + (yVel * t) - (.5 * 9.8 * t * t);
	}
}

//void Shape::show() {
//Show the dot
//apply_surface(box.x - camera.x, box.y - camera.y, dot, screen);
//}

void Shape::set_camera() {
	//Center the camera over the dot
	camera.x = (box.x + DOT_WIDTH / 2) - SCREEN_WIDTH / 2;
	camera.y = (box.y + DOT_HEIGHT / 2) - SCREEN_HEIGHT / 2;

	//Keep the camera in bounds.
	if (camera.x < 0) {
		camera.x = 0;
	}
	if (camera.y < 0) {
		camera.y = 0;
	}
	if (camera.x > LEVEL_WIDTH - camera.w) {
		camera.x = LEVEL_WIDTH - camera.w;
	}
	if (camera.y > LEVEL_HEIGHT - camera.h) {
		camera.y = LEVEL_HEIGHT - camera.h;
	}
}

int Shape::get_x() {
	return box.x;
}

int Shape::get_y() {
	return box.y;
}

int Shape::get_type() {
	return type;
}

SDL_Rect Shape::get_box() {
	return box;
}

